package com.example.cloudmusic.home.adapter

import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView

/**
 * 基础列表适配器，提供统一的点击事件处理和DiffUtil支持
 */
abstract class BaseListAdapter<T, VH : RecyclerView.ViewHolder>(
    diffCallback: DiffUtil.ItemCallback<T>
) : ListAdapter<T, VH>(diffCallback) {
    
    private var onItemClickListener: ((Int, T) -> Unit)? = null

    /**
     * 设置点击事件监听器
     */
    fun setOnItemClickListener(listener: (Int, T) -> Unit) {
        onItemClickListener = listener
    }

    /**
     * 处理item点击事件
     */
    protected fun onItemClick(position: Int) {
        if (position != RecyclerView.NO_POSITION) {
            getItem(position)?.let { item ->
                onItemClickListener?.invoke(position, item)
            }
        }
    }

    open fun setData(newItems: List<T>) {
        submitList(newItems)
    }
} 